package cn.wikiflyer.jft.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import cn.wk.libs4a.utils.L;

/**
 * 照片工具类：
 */
public class PhotoUtil {
	
	private static final int Code_PHOTO = 1;
	private static final int Code_CAMERA = 2;
	private static final int Code_CROP = 3;
	private static final String WK_CROP_FILE = "WK_CROP_FILE.jpg";
	
	public interface IImageCallBack{
		public void onResult(Bitmap bitmap,String path);
	}
	
	/**
	 * 从相册中取照片
	 */
	public void clickPhoto(Activity activity){
		Intent intent1 = new Intent(Intent.ACTION_PICK, null);
		intent1.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
		activity.startActivityForResult(intent1, Code_PHOTO);
	}
	/**
	 * 通过拍照获取照片
	 */
	public void clickCamera(Activity activity){
		Intent intent2 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		intent2.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(),WK_CROP_FILE)));
		activity.startActivityForResult(intent2, Code_CAMERA);
	}

	public void onActivityResult(Activity activity,int requestCode, int resultCode, Intent data,IImageCallBack callback){
		switch (requestCode) {
		case Code_PHOTO:
			if (resultCode == Activity.RESULT_OK) {
				cropPhoto(activity,data.getData());
			}
			break;
		case Code_CAMERA:
			if (resultCode == Activity.RESULT_OK) {
				File file = new File(Environment.getExternalStorageDirectory(),WK_CROP_FILE);
				cropPhoto(activity, Uri.fromFile(file));
			}
			break;
		case Code_CROP:
			if (data != null) {
				Bundle extras = data.getExtras();
				Bitmap bmp_result = extras.getParcelable("data");
				setPicToSD(bmp_result);
				callback.onResult(bmp_result,Environment.getExternalStorageDirectory().toString()+"/jft/"+WK_CROP_FILE);
			}
			break;
		default:
			break;
		}
	}
	/**
	 * 裁剪照片
	 */
	private void cropPhoto(Activity activity,Uri uri){
		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setDataAndType(uri, "image/*");
		intent.putExtra("crop", "true");
		// aspectX aspectY 是宽高的比例  
		intent.putExtra("aspectX", 1);
		intent.putExtra("aspectY", 1);
		// outputX outputY 是裁剪图片宽高
		intent.putExtra("outputX", 150);
		intent.putExtra("outputY", 150);
		intent.putExtra("return-data", true);
		activity.startActivityForResult(intent, Code_CROP);
	}
	/**
	 * 将图片保存到SD卡
	 */
	private void setPicToSD(Bitmap mBitmap){
		String sdStatus = Environment.getExternalStorageState();
		if (!Environment.MEDIA_MOUNTED.equals(sdStatus)) {
			return;		//检测到sd卡不可用
		}
		String path=Environment.getExternalStorageDirectory().toString()+"/jft/";//sd路径
		FileOutputStream fileOut = null;
		File file= new File(path);
		if(!file.exists()){
			file.mkdirs();	//创建文件夹
		}
		String fileName = path + WK_CROP_FILE;	//图片名字
		L.wj("imageName : "+fileName);
		try {
			fileOut = new FileOutputStream(fileName);
			mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOut);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}finally{
			//关闭流
			try {
				fileOut.flush();
				fileOut.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	
	
}
